package com.media.themovieworld.util.network.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import com.media.themovieworld.MovieWorldApp;

import android.provider.Settings.Secure;

public class IOUtil {

    // ===================================================================
    // Constants
    // ===================================================================
    private static final String LOG_TAG = IOUtil.class.getSimpleName();

    // ===================================================================
    // Fields
    // ===================================================================

    // ===================================================================
    // Constructors
    // ===================================================================

    // ===================================================================
    // Getter & Setter
    // ===================================================================

    // ===================================================================
    // Methods from BaseClass & Interfaces
    // ===================================================================

    // ===================================================================
    // Methods
    // ===================================================================
    public static File createFile(String pPath, String pFileName) throws IOException {
        File file = new File(pPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(pPath, pFileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }

    public static String inputStreamToString(InputStream is) throws IOException {
        String data = null;
        Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        data = IOUtil.readerToString(reader);
        return data;
    }

    public static String fileToString(File pFile) throws IOException {
        String data = null;
        Reader reader = new BufferedReader(new FileReader(pFile));
        data = IOUtil.readerToString(reader);
        return data;
    }
    public static String getDeviceId() {
    	return Secure.getString(MovieWorldApp.getAppContext().getContentResolver(),
                Secure.ANDROID_ID); 
    }

    private static String readerToString(Reader pReader) throws IOException {
        String data = null;
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        int n = 0;
        while ((n = pReader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
        data = writer.toString();
        writer.close();
        return data;
    }

    // ===================================================================
    // Methods for SubClass
    // ===================================================================

    // ===================================================================
    // Inner and Anonymous Classes
    // ===================================================================
}
